package com.mmall.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FTPUtil {

	private static Logger logger=LoggerFactory.getLogger(FTPUtil.class);
	
	private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
    private static String ftpPass = PropertiesUtil.getProperty("ftp.pass");

	private String ip;
    private int port;
    private String user;
    private String pwd;
    private FTPClient ftpClient;

    public FTPUtil(String ip,int port,String user,String pwd){
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pwd = pwd;
    }
    
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }
	
    //连接FTP服务器方法
    private boolean connectServer(String ip,int port,String user,String pwd){
    	
    	boolean isSuccess=false;
    	ftpClient=new FTPClient();
    	try {
			ftpClient.connect(ip,port);
			isSuccess=ftpClient.login(ftpUser, ftpPass);
		} catch (Exception e) {
			logger.error("连接FTP服务器异常",e);
		}
    	return isSuccess;
    }
    
    //内部实现的文件上传方法，用List支持文件批量上传
    private boolean uploadFile(String remotePath,List<File> fileList) throws IOException{
    	
    	boolean uploaded=true;
    	FileInputStream fileInputStream=null;
    	//如果连接FTP服务器成功
    	if(connectServer(this.ip, this.port, this.user, this.pwd)){
    		try {
    			//转到指定文件夹
				ftpClient.changeWorkingDirectory(remotePath);
				//设置缓存区
				ftpClient.setBufferSize(1024);
				//设置文件字符集
				ftpClient.setControlEncoding("UTF-8");
				//设置文件上传的类型为二进制
				ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
				//开启本地数据连接端口，否则无法成功上传
				ftpClient.enterLocalPassiveMode();
				for (File file : fileList) {
					fileInputStream=new FileInputStream(file);
					ftpClient.storeFile(file.getName(), fileInputStream);
				}
			} catch (Exception e) {
				logger.error("上传文件异常",e);
                uploaded = false;
                e.printStackTrace();
            } finally {
                fileInputStream.close();
                ftpClient.disconnect();
            }
    	}
    	return uploaded;
    }
    
    //外部调用的文件上传方法
    public static boolean uploadFile(List<File> fileList) throws IOException{
    	
    	FTPUtil ftpUtil=new FTPUtil(ftpIp, 21, ftpUser, ftpPass);
    	logger.info("开始连接ftp服务器");
    	boolean result=ftpUtil.uploadFile("img", fileList);
    	return result;
    	
    }
    
    
}
